package com.ebraga.trackerclub.android.manager.core;

import java.util.List;

import com.ebraga.trackerclub.android.dao.api.RankingDao;
import com.ebraga.trackerclub.android.dao.entity.RankingAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.dao.BackofficeDaoException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeIllegalArgumentManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.manager.api.RankingManager;
import com.ebraga.trackerclub.android.util.DLog;

public abstract class RankingManagerImpl implements RankingManager {
	
	protected RankingDao   rankingDao;
		
	protected void setRankingDao (RankingDao rankingDao){
		this.rankingDao = rankingDao;
	}		

	public void insert(RankingAndroid ranking) 
			throws BackofficeIllegalArgumentManagerException,
			BackofficeDBConnectionException,
			BackofficeManagerException {
		
		if (ranking == null){
			throw new BackofficeIllegalArgumentManagerException("Usuario invalido");
		}
		
		if (ranking.getName() == null){
			throw new BackofficeIllegalArgumentManagerException("Codigo do Usuario nulo");
		}
		
		
		try{
			rankingDao.insert(ranking);
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeDBConnectionException ("Erro ao conectar banco de dados");
		}catch(BackofficeDaoException e){
			DLog.e(e);
			throw new BackofficeManagerException (e.getMessage());
		}
		
	}

	public List<RankingAndroid> retrieveRanking()
			throws BackofficeIllegalArgumentManagerException,
			BackofficeDBConnectionException,
			BackofficeManagerException {
		
		List<RankingAndroid> rankingLst = null;
		try {
			rankingLst = rankingDao.retrieveRanking();
			return rankingLst;
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new  BackofficeDBConnectionException ("Erro ao conectar banco de dados");
		} catch (BackofficeDaoException e) {
			DLog.e(e);
			throw new BackofficeManagerException("Erro ao recuperar ultima posição do usuario");
		}
		
	}
	
	public void deleteAllLines() 
			throws BackofficeIllegalArgumentManagerException,
			BackofficeDBConnectionException,
			BackofficeManagerException {
		
		try{
			rankingDao.deleteAllLines();
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeDBConnectionException ("Erro ao conectar banco de dados");
		}catch(BackofficeDaoException e){
			DLog.e(e);
			throw new BackofficeManagerException (e.getMessage());
		}
		
	}	

}
